{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import heapq\n",
    "\n",
    "def beam_search(probs, k):\n",
    "    seq_scores = [[list(), 1.0]]\n",
    "    for prob in probs:\n",
    "        cands = list()\n",
    "        for i in range(len(seq_scores)):\n",
    "            seq, score = seq_scores[i]\n",
    "            for j in range(len(prob)):\n",
    "                cand = [seq+[j], score * prob[j]]\n",
    "                cands.append(cand)\n",
    "        seq_scores = sorted(cands, key=lambda x: x[1], reverse=True)[:k]\n",
    "    seq, score = seq_scores[0]\n",
    "    return seq\n",
    "\n",
    "def beam_search2(probs, k):\n",
    "    seqs, scores = [[]], [1.0]\n",
    "    for prob in probs:\n",
    "        beam_seqs, beams_scores = [], []\n",
    "        for i in range(len(seqs)):\n",
    "            seq, score = seqs[i], scores[i]\n",
    "            for j in range(len(prob)):\n",
    "                beam_seqs.append(seq + [j])\n",
    "                beams_scores.append(score * prob[j])\n",
    "        ind = np.argpartition(beams_scores, -k)[-k:]\n",
    "        seqs = np.array(beam_seqs)[ind].tolist()\n",
    "        scores = np.array(beams_scores)[ind].tolist()\n",
    "    ind = np.argmax(scores)\n",
    "    seq = np.array(seqs)[ind]\n",
    "    return seq\n",
    "\n",
    "def beam_search3(probs, k):\n",
    "    seq_scores = [[list(), 1.0]]\n",
    "    for prob in probs:\n",
    "        cands = list()\n",
    "        for i in range(len(seq_scores)):\n",
    "            seq, score = seq_scores[i]\n",
    "            for j in range(len(prob)):\n",
    "                cand = [seq + [j], score * prob[j]]\n",
    "                cands.append(cand)\n",
    "        seq_scores = heapq.nlargest(k, cands, lambda d: d[1])\n",
    "    seq, score = seq_scores[0]\n",
    "    return seq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 1, 2, 0, 1]\n"
     ]
    }
   ],
   "source": [
    "data = [[0.1, 0.2, 0.4, 0.3],\n",
    "        [0.3, 0.5, 0.15, 0.05],\n",
    "        [0.25, 0.2, 0.3, 0.25],\n",
    "        [0.5, 0.3, 0.08, 0.12],\n",
    "        [0.1, 0.4, 0.3, 0.2]]\n",
    "data = np.array(data)\n",
    "\n",
    "seq = beam_search(data, 3)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 1 2 0 1]\n"
     ]
    }
   ],
   "source": [
    "seq = beam_search2(data, 3)\n",
    "print(seq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 1, 2, 0, 1]\n"
     ]
    }
   ],
   "source": [
    "seq = beam_search3(data, 3)\n",
    "print(seq)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}